home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 4: GNU Archives / Linux Cubed Series 4 - GNU Archives.iso / gnu / gawk-3.000 / gawk-3 / gawk-3.0.0 / re.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-19  |  6.4 KB  |  306 lines

  1. /*
  2.  * re.c - compile regular expressions.
  3.  */
  4.  
  5. /* 
  6.  * Copyright (C) 1991-1995 the Free Software Foundation, Inc.
  7.  * 
  8.  * This file is part of GAWK, the GNU implementation of the
  9.  * AWK Programming Language.
  10.  * 
  11.  * GAWK is free software; you can redistribute it and/or modify
  12.  * it under the terms of the GNU General Public License as published by
  13.  * the Free Software Foundation; either version 2 of the License, or
  14.  * (at your option) any later version.
  15.  * 
  16.  * GAWK is distributed in the hope that it will be useful,
  17.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  19.  * GNU General Public License for more details.
  20.  * 
  21.  * You should have received a copy of the GNU General Public License
  22.  * along with this program; if not, write to the Free Software
  23.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA
  24.  */
  25.  
  26. #include "awk.h"
  27.  
  28. static reg_syntax_t syn;
  29.  
  30. /* make_regexp --- generate compiled regular expressions */
  31.  
  32. Regexp *
  33. make_regexp(s, len, ignorecase, dfa)
  34. char *s;
  35. size_t len;
  36. int ignorecase;
  37. int dfa;
  38. {
  39.     Regexp *rp;
  40.     const char *rerr;
  41.     char *src = s;
  42.     char *temp;
  43.     char *end = s + len;
  44.     register char *dest;
  45.     register int c, c2;
  46.  
  47.     /* Handle escaped characters first. */
  48.  
  49.     /*
  50.      * Build a copy of the string (in dest) with the
  51.      * escaped characters translated, and generate the regex
  52.      * from that.  
  53.      */
  54.     emalloc(dest, char *, len + 2, "make_regexp");
  55.     temp = dest;
  56.  
  57.     while (src < end) {
  58.         if (*src == '\\') {
  59.             c = *++src;
  60.             switch (c) {
  61.             case 'a':
  62.             case 'b':
  63.             case 'f':
  64.             case 'n':
  65.             case 'r':
  66.             case 't':
  67.             case 'v':
  68.             case 'x':
  69.             case '0':
  70.             case '1':
  71.             case '2':
  72.             case '3':
  73.             case '4':
  74.             case '5':
  75.             case '6':
  76.             case '7':
  77.                 c2 = parse_escape(&src);
  78.                 if (c2 < 0)
  79.                     cant_happen();
  80.                 /*
  81.                  * Unix awk treats octal (and hex?) chars
  82.                  * literally in re's, so escape regexp
  83.                  * metacharacters.
  84.                  */
  85.                 if (do_traditional && ! do_posix && (isdigit(c) || c == 'x')
  86.                     && strchr("()|*+?.^$\\[]", c2) != NULL)
  87.                     *dest++ = '\\';
  88.                 *dest++ = (char) c2;
  89.                 break;
  90.             case 'y':    /* normally \b */
  91.                 /* gnu regex op */
  92.                 if (! do_traditional) {
  93.                     *dest++ = '\\';
  94.                     *dest++ = 'b';
  95.                     src++;
  96.                     break;
  97.                 }
  98.                 /* else, fall through */
  99.             default:
  100.                 *dest++ = '\\';
  101.                 *dest++ = (char) c;
  102.                 src++;
  103.                 break;
  104.             } /* switch */
  105.         } else
  106.             *dest++ = *src++;    /* not '\\' */
  107.     } /* for */
  108.  
  109.     *dest = '\0' ;    /* Only necessary if we print dest ? */
  110.     emalloc(rp, Regexp *, sizeof(*rp), "make_regexp");
  111.     memset((char *) rp, 0, sizeof(*rp));
  112.     rp->pat.allocated = 0;    /* regex will allocate the buffer */
  113.     emalloc(rp->pat.fastmap, char *, 256, "make_regexp");
  114.  
  115.     if (ignorecase)
  116.         rp->pat.translate = casetable;
  117.     else
  118.         rp->pat.translate = NULL;
  119.     len = dest - temp;
  120.     if ((rerr = re_compile_pattern(temp, len, &(rp->pat))) != NULL)
  121.         fatal("%s: /%s/", rerr, temp);
  122.  
  123.     /* gack. this must be done *after* re_compile_pattern */
  124.     rp->pat.newline_anchor = FALSE; /* don't get \n in middle of string */
  125.     if (dfa && ! ignorecase) {
  126.         dfacomp(temp, len, &(rp->dfareg), TRUE);
  127.         rp->dfa = TRUE;
  128.     } else
  129.         rp->dfa = FALSE;
  130.  
  131.     free(temp);
  132.     return rp;
  133. }
  134.  
  135. /* research --- do a regexp search. use dfa if possible */
  136.  
  137. int
  138. research(rp, str, start, len, need_start)
  139. Regexp *rp;
  140. register char *str;
  141. int start;
  142. register size_t len;
  143. int need_start;
  144. {
  145.     char *ret = str;
  146.     int try_backref;
  147.  
  148.     /*
  149.      * Always do dfa search if can; if it fails, then even if
  150.      * need_start is true, we won't bother with the regex search.
  151.      */
  152.     if (rp->dfa) {
  153.         char save;
  154.         int count = 0;
  155.  
  156.         /*
  157.          * dfa likes to stick a '\n' right after the matched
  158.          * text.  So we just save and restore the character.
  159.          */
  160.         save = str[start+len];
  161.         ret = dfaexec(&(rp->dfareg), str+start, str+start+len, TRUE,
  162.                     &count, &try_backref);
  163.         str[start+len] = save;
  164.     }
  165.     if (ret) {
  166.         if (need_start || rp->dfa == FALSE || try_backref) {
  167.             int result = re_search(&(rp->pat), str, start+len,
  168.                     start, len, &(rp->regs));
  169.             /* recover any space from C based alloca */
  170. #ifdef C_ALLOCA
  171.             (void) alloca(0);
  172. #endif
  173.             return result;
  174.         } else
  175.             return 1;
  176.     } else
  177.         return -1;
  178. }
  179.  
  180. /* refree --- free up the dynamic memory used by a compiled regexp */
  181.  
  182. void
  183. refree(rp)
  184. Regexp *rp;
  185. {
  186.     free(rp->pat.buffer);
  187.     free(rp->pat.fastmap);
  188.     if (rp->regs.start)
  189.         free(rp->regs.start);
  190.     if (rp->regs.end)
  191.         free(rp->regs.end);
  192.     if (rp->dfa)
  193.         dfafree(&(rp->dfareg));
  194.     free(rp);
  195. }
  196.  
  197. /* dfaerror --- print an error message for the dfa routines */
  198.  
  199. void
  200. dfaerror(s)
  201. const char *s;
  202. {
  203.     fatal("%s", s);
  204. }
  205.  
  206. /* re_update --- recompile a dynamic regexp */
  207.  
  208. Regexp *
  209. re_update(t)
  210. NODE *t;
  211. {
  212.     NODE *t1;
  213.  
  214. /* #    define    CASE    1 */
  215.     if ((t->re_flags & CASE) == IGNORECASE) {
  216.         if ((t->re_flags & CONST) != 0)
  217.             return t->re_reg;
  218.         t1 = force_string(tree_eval(t->re_exp));
  219.         if (t->re_text != NULL) {
  220.             if (cmp_nodes(t->re_text, t1) == 0) {
  221.                 free_temp(t1);
  222.                 return t->re_reg;
  223.             }
  224.             unref(t->re_text);
  225.         }
  226.         t->re_text = dupnode(t1);
  227.         free_temp(t1);
  228.     }
  229.     if (t->re_reg != NULL)
  230.         refree(t->re_reg);
  231.     if (t->re_cnt > 0)
  232.         t->re_cnt++;
  233.     if (t->re_cnt > 10)
  234.         t->re_cnt = 0;
  235.     if (t->re_text == NULL) {
  236.         t1 = force_string(tree_eval(t->re_exp));
  237.         t->re_text = dupnode(t1);
  238.         free_temp(t1);
  239.     }
  240.     t->re_reg = make_regexp(t->re_text->stptr, t->re_text->stlen,
  241.                 IGNORECASE, t->re_cnt);
  242.     t->re_flags &= ~CASE;
  243.     t->re_flags |= IGNORECASE;
  244.     return t->re_reg;
  245. }
  246.  
  247. /* resetup --- choose what kind of regexps we match */
  248.  
  249. void
  250. resetup()
  251. {
  252.     if (do_posix)
  253.         syn = RE_SYNTAX_POSIX_AWK;    /* strict POSIX re's */
  254.     else if (do_traditional)
  255.         syn = RE_SYNTAX_AWK;        /* traditional Unix awk re's */
  256.     else
  257.         syn = RE_SYNTAX_GNU_AWK;    /* POSIX re's + GNU ops */
  258.  
  259.     /*
  260.      * Interval expressions are off by default, since it's likely to
  261.      * break too many old programs to have them on.
  262.      */
  263.     if (do_intervals)
  264.         syn |= RE_INTERVALS;
  265.  
  266.     (void) re_set_syntax(syn);
  267.     dfasyntax(syn, FALSE);
  268. }
  269.  
  270. /* avoid_dfa --- temporary kludge function until we have a new dfa.c */
  271.  
  272. int
  273. avoid_dfa(re, str, len)
  274. NODE *re;
  275. char *str;
  276. size_t len;
  277. {
  278.     char *restr;
  279.     int relen;
  280.     int anchor, i;
  281.     char *end;
  282.  
  283.     if ((re->re_flags & CONST) != 0) {
  284.         restr = re->re_exp->stptr;
  285.         relen = re->re_exp->stlen;
  286.     } else {
  287.         restr = re->re_text->stptr;
  288.         relen = re->re_text->stlen;
  289.     }
  290.  
  291.     for (anchor = FALSE, i = 0; i < relen; i++) {
  292.         if (restr[i] == '^' || restr[i] == '$') {
  293.             anchor = TRUE;
  294.             break;
  295.         }
  296.     }
  297.     if (! anchor)
  298.         return FALSE;
  299.  
  300.     for (end = str + len; str < end; str++)
  301.         if (*str == '\n')
  302.             return TRUE;
  303.  
  304.     return FALSE;
  305. }
  306.